<template>
  <page-header-wrapper>
    <a-card :bordered="false">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="4" :sm="24">
              <a-form-item>
                <a-input type="text" placeholder="订单号"/>
              </a-form-item>
            </a-col>
            <a-col :md="4" :sm="24">
              <a-form-item>
                <a-input type="text" placeholder="VIN"/>
              </a-form-item>
            </a-col>
            <a-col :md="4" :sm="24">
              <a-form-item>
                <a-range-picker :locale="locale" @change="onChange"/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <span class="table-page-search-submitButtons">
                <a-input-search v-model="queryParam.search" placeholder="请输入关键字查询" enter-button="查询" @search="$refs.table.refresh(true)"/>
                <a-button style="margin-left: 8px" @click="() => this.queryParam = {}">重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>

      <div class="table-operator">
        <a-button v-action:add type="primary" icon="plus" @click="handleAdd">新建</a-button>
        <a-button v-action:update v-if="selectedRowKeys.length > 0" type="primary" icon="delete" @click="handleAdd">删除</a-button>
        <a-button v-action:update v-if="selectedRowKeys.length > 0" type="primary" icon="lock" @click="handleAdd">锁定</a-button>
      </div>

      <s-table
        ref="table"
        size="default"
        rowKey="id"
        :columns="columns"
        :data="loadData"
        :alert="true"
        :rowSelection="rowSelection"
        showPagination="auto"
      >
        <!--状态插槽-->
        <span slot="status" slot-scope="text">
          <a-tag :color="text | statusFilter">{{ text }}</a-tag>
        </span>
        <!--头像插槽-->
        <span slot="avatar" slot-scope="text">
          <a-avatar :src="`data:image/png;base64,${text}`"/>
        </span>
        <!--操作插槽-->
        <span slot="action" slot-scope="text, record">
          <template>
            <a @click="handleEdit(record)" icon="edit">编辑</a>
            <a-divider type="vertical"/>
            <a @click="handleSub(record)">删除</a>
          </template>
        </span>
      </s-table>

    </a-card>
  </page-header-wrapper>
</template>

<script>
  import { Ellipsis, STable } from '@/components'
  import { getUserList, saveOrUpdate } from '@/api/user'
  import md5 from 'md5'
  import { STATUS_MAP } from '@/constants/commonConstant'
  // 默认语言为 en-US，如果你需要设置其他语言，推荐在入口文件全局设置 locale
  import moment from 'moment'
  import 'moment/locale/zh-cn'
  import locale from 'ant-design-vue/es/date-picker/locale/zh_CN'

  export default {
    name: 'UserList',
    components: {
      STable,
      Ellipsis
    },
    data () {
      return {
        moment,
        locale,
        columns: [
          {
            title: 'ID',
            dataIndex: 'id'
          },
          {
            title: '销售订单号',
            dataIndex: 'name'
          },
          {
            title: 'VIN',
            dataIndex: 'describe'
          },
          {
            title: 'First Name',
            dataIndex: 'name'
          },
          {
            title: '实收综合',
            dataIndex: 'name'
          },
          {
            title: '利润',
            dataIndex: 'name'
          },
          {
            title: '日期',
            dataIndex: 'createTime'
          },
          {
            title: '操作',
            dataIndex: 'action',
            width: '150px',
            scopedSlots: { customRender: 'action' }
          }
        ],
        // create model
        visible: false,
        confirmLoading: false,
        mdl: null,
        // 高级搜索 展开/关闭
        advanced: false,
        // 查询参数
        queryParam: {},
        // 加载数据方法 必须为 Promise 对象
        loadData: parameter => {
          const requestParameters = Object.assign({}, parameter, this.queryParam)
          return getUserList(requestParameters).then(res => {
            return res.result
          })
        },
        selectedRowKeys: [],
        selectedRows: []
      }
    },
    filters: {
      statusFilter (type) {
        return STATUS_MAP[type]
      }
    },
    computed: {
      rowSelection () {
        return {
          selectedRowKeys: this.selectedRowKeys,
          onChange: this.onSelectChange
        }
      }
    },
    methods: {
      onChange (date, dateString) {
        console.log(date, dateString)
      },
      handleAdd () {
        this.mdl = null
        this.visible = true
      },
      handleEdit (record) {
        this.visible = true
        this.mdl = { ...record }
      },
      handleOk () {
        const form = this.$refs.createModal.form
        this.confirmLoading = true
        form.validateFields((errors, values) => {
          if (!errors) {
            values.id = values.id > 0 ? values.id : null
            values.password = values.password && md5(values.password)
            saveOrUpdate(values).then(response => {
              if (response.success) {
                this.visible = false
                this.confirmLoading = false
                // 重置表单数据
                form.resetFields()
                // 刷新表格
                this.$refs.table.refresh()
                this.$message.info(values.id > 0 ? '修改成功' : '新增成功')
              } else {
                this.confirmLoading = false
                this.$message.error(response.message)
              }
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      handleCancel () {
        this.visible = false
        const form = this.$refs.createModal.form
        form.resetFields() // 清理表单数据（可不做）
      },
      handleSub (record) {
        if (record.status !== 0) {
          this.$message.info(`${record.no} 订阅成功`)
        } else {
          this.$message.error(`${record.no} 订阅失败，规则已关闭`)
        }
      },
      onSelectChange (selectedRowKeys, selectedRows) {
        this.selectedRowKeys = selectedRowKeys
        this.selectedRows = selectedRows
      }
    }
  }
</script>
